<template>
  <div class="input-body">
    <t-input
      ref="inputRef"
      class="todo-title"
      v-model="title"
      :maxlength="50"
      show-limit-number
      clearable
      autofocus
      :placeholder="placeholder"
      @enter="onEnter"
    />
  </div>
</template>

<script lang="ts" setup>
import { useTaskStore } from "@/store";
import { MessagePlugin } from "tdesign-vue-next";
import { ref } from "vue";

defineProps({
  placeholder: {
    type: String,
    default: "+ 创建任务标题，按下回车快速创建",
  },
});

const taskStore = useTaskStore();
const inputRef = ref(null);
const title = ref("");

// enter回调事件
const onEnter = () => {
  if (!title.value) {
    MessagePlugin.warning("任务内容必填");
    return;
  }
  // 创建任务
  taskStore.handleCreateTask({ title: title.value });
  // 输入框失去焦点
  inputRef.value.blur();
  // 清除输入的任务
  title.value = "";
};
</script>

<style lang="less" scoped>
.input-body {
  max-width: 600px;
  padding: 12px;
  box-sizing: border-box;
  background: var(--td-brand-color-4);
  border-radius: 4px;
  box-shadow: var(--td-shadow-1);
  margin: 0 auto;
  .todo-title {
    width: 100%;
    border-radius: 12px;
    :deep(.t-input) {
      background: var(--td-brand-color-1);
      .t-input__suffix {
        .t-input__limit-number {
          background: var(--td-brand-color-1);
        }
      }
    }
  }
  .operations {
    margin-top: 12px;
    width: 100%;
    overflow: hidden;
    margin-top: 12px;
    margin-top: px;
    display: flex;
    justify-content: flex-end;
    .button {
      margin-left: 12px;
    }
  }
}
</style>